package nl.hyves.api.parameters
{
	import com.adobe.cairngorm.contract.Contract;
	public class GlobalParameters implements IParameters
	{
		private static const MAX_RESULTS_PER_PAGE:int = 500;
		private var _parameters:Object;
		
		public function get parameters():Object
		{
			return _parameters;
		}
		
		public function GlobalParameters(parameters:Object = null):void
		{
			if (parameters is IParameters)
			{
				_parameters = IParameters(parameters).parameters;
			}
			else if (parameters)
			{
				_parameters = parameters;
			}
			else
			{
				_parameters = new Object();
			}
			
			Contract.postcondition(this.parameters != null);
		}
		
		public function page(value:int):GlobalParameters
		{
			Contract.precondition(value > 0);
			
			_parameters.ha_page = value;
			
			return this;
		}
		
		public function resultsPerPage(value:int):GlobalParameters
		{
			Contract.precondition(value > 0);
			
			_parameters.ha_resultsperpage = Math.min(value, MAX_RESULTS_PER_PAGE);	// value > 500 throws an API error.
			
			return this;
		}
		
		public function responseFields(...values):GlobalParameters
		{
			Contract.precondition(values && values.length > 0);
			
			_parameters.ha_responsefields = values.join(',');
			
			return this;
		}
		
		public function returnFields(...values):GlobalParameters
		{
			Contract.precondition(values && values.length > 0);
			
			_parameters.ha_returnfields = values.join(',');
			
			return this;
		}
	}
}
